import UploadFile from './UploadFile'

export default class UploadImageAdapter {
  constructor(vue, loader) {
    this.vm = vue
    this.loader = loader
    this.qiniuData = {
      config: {
        useCdnDomain: true
        // region: 'qiniu.region.z2'
      },
      token: '',
      putExtra: {
        fname: this.loader.file.name,
        params: {},
        mimeType: null
      }
    }
  }

  async getOSSInfo() {
    let res = await this.vm.$http({
      url: this.vm.$http.adornUrl('/sys/oss/imguptoken'),
      method: 'post'
    })
    return res.data
  }

  async upload() {
    const img = await this.loader.file
    let ossInfo = await this.getOSSInfo()
    let uploadFile = new UploadFile(img, ossInfo.token, ossInfo.domain)

    return new Promise((resolve, reject) => {
      uploadFile.upload({
        onSuccess: file => {
          resolve({ default: file.url })
        }
      })
    })
  }

  abort() {
    // 可以书写删除服务器图片的逻辑
  }
}
